package cn.fzhui;

import java.text.DecimalFormat;
import java.util.Random;
import java.util.Scanner;

/**
 * 数组元素计算
 */
public class ArrayCal {

    private double[][] array = new double[16][16];
    private double[][] result = new double[array.length + 2][array.length + 2];
    private static DecimalFormat df = new DecimalFormat("##00.00#");
    private static Random r = new Random();

    /**
     * 构造函数
     */
    public ArrayCal() {
        // 设置格式化格式
        df.setMaximumFractionDigits(2);
    }

    /**
     * 生成数组数据
     *
     * @param inputType
     */
    public void generate(InputType inputType) {
        switch (inputType) {
            case INPUT:
                this.inputGenerate();
                break;
            case AUTO:
                this.autoGenerate();
                break;
            default:
                System.out.println("没有该方法！");
        }
    }

    /**
     * 手动输入数组数据
     */
    private void inputGenerate() {
        Scanner scan = new Scanner(System.in);
        for (int row = 0; row < this.array.length; row++) {
            for (int column = 0; column < this.array.length; column++) {
                System.out.print("请输入位于第[" + row + "]行第[" + column + "]列的数字：");
                this.array[row][column] = scan.nextDouble();
            }
        }
    }

    /**
     * 自动生成数组数据
     */
    private void autoGenerate() {
        System.out.println("正在在生成数组数据...");
        for (int row = 0; row < this.array.length; row++) {
            for (int column = 0; column < this.array.length; column++) {
                this.array[row][column] = this.random();
            }
        }
    }

    /**
     * 生成随机数
     *
     * @return
     */
    private Double random() {
        return Double.valueOf(df.format(r.nextInt(10) + r.nextDouble()));
    }

    /**
     * 输出
     */
    public void output() {
        // 数组拷贝，偏移
        this.copy();

        int lastRowIndex = this.result.length - 1;

        for (int row = 0; row < this.result.length; row++) {
            for (int column = 0; column < this.result[row].length; column++) {

                // 最后一列的下标
                int lastColumnIndex = this.result[row].length - 1;

                // 每个单元格的值
                double value = this.result[row][column];

                // 行累加结果，忽略最后一行的计算，因为要计算左上到右下
                if (row < lastRowIndex) {
                    result[row][lastColumnIndex] = Double.parseDouble(df.format(result[row][lastColumnIndex] + value));
                }

                // 列累加结果，忽略最后一列的计算，因为要计算左上到右下
                if (column < lastColumnIndex) {
                    result[lastRowIndex][column] = Double.parseDouble(df.format(result[lastRowIndex][column] + value));
                }

                if (row >= 1 && column == row) {
                    // 计算左上到右下的累加
                    this.result[lastRowIndex][lastColumnIndex] = Double.parseDouble(df.format(this.result[lastRowIndex][lastColumnIndex] + this.result[row][row]));

                    // 计算右上到左下的累加
                    this.result[lastRowIndex][0] = Double.parseDouble(df.format(this.result[lastRowIndex][0] + this.result[row][lastColumnIndex - row]));
                }

                // 补全空格对齐
                StringBuilder out = new StringBuilder();
                String str = "━";

                if (row != 0) {
                    str = String.valueOf(value);
                }

                if (column == 0 && row != lastRowIndex) {
                    if (row == 0) {
                        str = "┏";
                    } else {
                        str = "┃";
                    }
                }

                for (int i = 0; i < 20 - str.length(); i++) {
                    if (row == 0 & column != 0) {
                        out.append("━");
                    } else {
                        out.append(" ");
                    }
                }

                // 是否结果列，如果是则输出结果
                System.out.print(out + str);

            }
            System.out.println();

        }

    }

    /**
     * 数组偏移拷贝
     */
    private void copy() {
        // 偏移量
        int x = 1, y = 1;
        for (int row = 0; row < this.array.length; row++) {
            for (int column = 0; column < this.array.length; column++) {
                this.result[row + x][column + y] = this.array[row][column];
            }
        }
    }
}
